package cn.jkgly.test.Controller;

import cn.jkgly.test.pojo.JSONResult;
import cn.jkgly.test.pojo.Resource;
import cn.jkgly.test.pojo.User;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.export.ExcelExportServer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

@RestController
public class HelloController {

    @Autowired
    private Resource resource;

    @RequestMapping("/getResource")
    public JSONResult getResource() {
        Resource bean=new Resource();
        BeanUtils.copyProperties(resource,bean);
        return JSONResult.ok(bean);
    }

    @RequestMapping("/hello")
    public Object hello() {
        System.out.println("呵呵呵呵呵呵");
        return "哈哈哈~";
    }


    @RequestMapping("/hello2")
    public Object hello2(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
        List<Map> list = new ArrayList<>();
        FileWriter fileWriter = null;
        File file=new File("D:\\肿瘤数据");
        if(!file.exists()){//如果文件夹不存在
            file.mkdir();//创建文件夹
        }
        StringBuffer sb = new StringBuffer();
        try {
            OutputStream os = httpServletResponse.getOutputStream();

           //创建文本文件
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.append("<Therioma>");
            sb.append("<rid/>");
            for (Map map : list) {
                Iterator<String> it = map.keySet().iterator();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    sb
                            .append("<").append(key).append(">")
                            .append(map.get(key))
                            .append("</").append(key).append(">");
                }
            }

            sb.append("</Therioma>");
           // os.write(sb);
                fileWriter.write(sb.toString());
                fileWriter.flush();
                fileWriter.close();
                os.flush();//清空缓冲区，并将内容写到浏览器上
                os.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
        return "成功";
    }


}
